package com.example.multithread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Description 循环屏障demo
 * @ClassName CyclicBarrierTest1
 * @Author Administrator
 * @Date 2020/5/7 20:56
 * @Version 1.0
 */
public class CyclicBarrierTest1 {
    private static CyclicBarrier cyclicBarrier;

    static class CyclicBarrierThread extends Thread{
        public void run() {
            try {
                Thread.sleep((long) (Math.random() * 5000));
                System.out.println(Thread.currentThread().getName() + "到了");
                //等待
                cyclicBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        cyclicBarrier = new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                System.out.println("人到齐了，开会吧....");
            }
        });

        for(int i = 0 ; i < 9 ; i++){
            new CyclicBarrierThread().start();
        }
        System.out.println("-------------------------");
    }
}
